﻿#include<pch.h>

using namespace std;
class Test {
public:
	Test(){
		
	}
	
	Test(const string& str) {
		pData = const_cast<char*>(str.c_str());
	}

	~Test()
	{

	}
	char& operator[](size_t index) const{
		return pData[index];
	}
	char& operator[](size_t index){
		return pData[index];
	}
	void setData(char* str) {
		pData = str;
	}
	char* show() {
		return pData;
	}
	size_t length() const;
private:
	char* pData;
	int index;
	mutable size_t textLength;
	mutable bool valid;
};
size_t Test::length() const {
	if (!valid) {
		textLength = strlen(pData);
		valid = true;
	}
	return textLength;
}


void logCall(const string& name) {
	printf(name.c_str());
}
class Customer {
public:
	Customer(){}
	virtual ~Customer(){}
	Customer(const Customer& rhs);
	Customer& operator=(const Customer& rhs);
private:
	string name;
};
Customer::Customer(const Customer& rhs):name(rhs.name) {
	logCall("Customer copy constructor");
}
Customer& Customer::operator=(const Customer& rhs){
	logCall("Customer copy assignment operator");
	name = rhs.name;
	return *this;
}

class PriorityCustomer : public Customer{
public:
	PriorityCustomer() {}
	~PriorityCustomer() {
	}
	PriorityCustomer(const PriorityCustomer& rhs);
	PriorityCustomer& operator=(const PriorityCustomer& rhs);
private:
	string priority;
};
PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs) 
	:Customer(rhs), priority(rhs.priority)
{
	logCall("PriorityCustomer copy constructor");
}
PriorityCustomer& PriorityCustomer::operator=(const PriorityCustomer& rhs) {
	logCall("PriorityCustomer copy assignment operator");
	Customer::operator=(rhs);
	priority = rhs.priority;
	return *this;
}


void outMem() {
	cout << "out menery";
}
//动态设置内存溢出处理器的模板类

class NewHandlerHolder {
public:
	explicit NewHandlerHolder(std::new_handler nh)
		:handler(nh) {

	}
	~NewHandlerHolder() {
		std::set_new_handler(handler);
	}
private:
	std::new_handler handler;
	NewHandlerHolder(const NewHandlerHolder& nh);
	NewHandlerHolder& operator =(const NewHandlerHolder& nh);
};

template<typename T>
class NewHandlerSupport{

public:
	static std::new_handler set_new_handler(std::new_handler p) throw();
	static void* operator new(std::size_t s) throw(std::bad_alloc);
private:
	static std::new_handler currentNewHandler;
};

template<typename T>
std::new_handler NewHandlerSupport<T>::set_new_handler(std::new_handler p) throw(){
	std::new_handler old = currentNewHandler;
	currentNewHandler = p;
	return old;
}

template<typename T>
void* NewHandlerSupport<T>::operator new(std::size_t s) throw(std::bad_alloc) {
	NewHandlerHolder h(currentNewHandler);
	return ::operator new(s);
}
template<typename T>
std::new_handler NewHandlerSupport<T>::currentNewHandler = 0;

//崩溃
class Lock{
public:
	explicit Lock(mutex* pm)
		:mutexPtr(pm) {
		lock();
	}
	~Lock() {
		unlock();
		cout << "释放锁" << endl;
	}
private:
	void lock() {
		mutexPtr.get()->lock();
	}
	void unlock() {
		mutexPtr.get()->unlock();
	}
	Lock(const Lock& l);
	Lock& operator =(const Lock& l);
	std::shared_ptr<mutex> mutexPtr;
};
mutex m;
void testClass() {

	//Lock m1(&m);

	std::string str = "string";
	char* s = const_cast<char*>(str.c_str());

	Test t;
	t.setData(s);
	char *ccc = &t[0];
	*ccc = 'J';
	std::cout << "test class:" << t.show();
	cout << endl;

	/*const Test t1(str);
	const char *ccc = &t1[0];
	*ccc = 'K';
	std::cout << "test2 class:" << t.show();
	cout << endl;*/
	//system("pause");
}
void main111() {
	mutex m;
	Lock m1(&m);
	//Lock m2(m1);//不予许复制
	Customer a;
	Customer b(a);
	Customer c = a;
	std::set_new_handler(outMem);

	


}